package server;

import org.apache.catalina.startup.Tomcat;

public class TomcatServer { 
	private static Tomcat tomcat = null; 
	private static String PROJECT_PATH = System.getProperty("user.dir");
	private static String CONTEXT_PATH = "/WebRoot";
	private static String WEB_APP_PATH = PROJECT_PATH + CONTEXT_PATH;
	private static String TOMCAT_HOME = PROJECT_PATH + "/Tomcat";
	private static String ENCODING = "UTF-8"; 
	private static int TOMCAT_PORT = 8080; 
	// 启动嵌入式Tomcat服务器 
	public static void startTomcat() throws Exception { 
		try { 
			long startTime = System.currentTimeMillis(); 
			tomcat = new Tomcat(); 
			tomcat.setBaseDir(TOMCAT_HOME); 
			tomcat.setPort(TOMCAT_PORT);
			tomcat.addWebapp(CONTEXT_PATH, WEB_APP_PATH);
			tomcat.enableNaming();//执行这句才能支持JDNI查找 
			tomcat.getConnector().setURIEncoding(ENCODING);
			tomcat.start(); 
			System.err.println("Tomcat started in " + (System.currentTimeMillis() - startTime) + " ms."); 
			tomcat.getServer().await();//让服务器一直跑 
			} catch (Exception e) { 
				e.printStackTrace(); 
			} 
		} 
	public static void main(String[] args) { 
		try { 
			TomcatServer.startTomcat(); 
		} catch (Exception e) { 
				e.printStackTrace(); 
		} 
	} 
}